/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2017
 */

package com.shop.pandian.app.controller;

import java.util.Hashtable;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.mysql.jdbc.StringUtils;

import com.shop.pandian.app.model.AcctRole;
import com.shop.pandian.app.service.AcctRoleService;
import com.shop.pandian.app.vo.query.AcctRoleQuery;
import com.shop.pandian.framework.base.controller.BaseSpringController;
import com.shop.pandian.framework.base.page.Page;
import com.shop.pandian.framework.base.web.Flash;
import com.shop.pandian.framework.base.web.HttpUtils;

/**
 * @author Xi Wei
 * @version 1.0
 * @since 1.0
 */
 
@Controller
@RequestMapping("acctRole")
public class AcctRoleController extends BaseSpringController{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	@Autowired
	private AcctRoleService acctRoleService;
	
	
	
	private final String LIST_ACTION = "redirect:/acctRole/list";
	
	public AcctRoleController() {
	}
	

	
	/** 
	 * 执行搜索 
	 **/
	@RequestMapping(value="/list")
	public ModelAndView list(HttpServletRequest request,AcctRoleQuery query) {
		String mode = request.getParameter("mode");
		if(!StringUtils.isNullOrEmpty(mode)){
			query = (AcctRoleQuery)request.getSession().getAttribute(AcctRoleQuery.class.getSimpleName());
		}
		
		Page page = this.acctRoleService.findPage(query);
		request.getSession().setAttribute(AcctRoleQuery.class.getSimpleName(), query);
		ModelAndView result = new ModelAndView("/AcctRole/list");
		ModelMap map = toModelMap(page, query);
		result.addAllObjects(map);
		return result;
	}
	
	/** 
	 * 查看对象
	 **/
	@RequestMapping(value="/show")
	public ModelAndView show(HttpServletRequest request) throws Exception {
		java.lang.Long id = new java.lang.Long(request.getParameter("id"));
		AcctRole acctRole = (AcctRole)acctRoleService.getById(id);
		if(acctRole == null){			
			Flash.current().error("error.record.notfound");
			return new ModelAndView("redirect:/acctRole/list");
		}
		return new ModelAndView("/AcctRole/show","acctRole",acctRole);
	}
	
	/** 
	 * 进入新增页面
	 **/
	@RequestMapping(value="/create")
	public ModelAndView create(AcctRole acctRole) throws Exception {
		ModelMap map = new ModelMap();
		map.addAttribute("acctRole", acctRole);
		
		
		return new ModelAndView("/AcctRole/create",map);
		
	}
	
	/** 
	 * 保存新增对象
	 **/
	@RequestMapping(value="/save")
	public ModelAndView save(@Valid AcctRole acctRole,BindingResult result) throws Exception {
		
		if(result.hasErrors()){
			//return new ModelAndView("/AcctRole/create","acctRole",acctRole);
			return create(acctRole);
		}
		
		
		acctRoleService.save(acctRole);
		Flash.current().success(CREATED_SUCCESS); //存放在Flash中的数据,在下一次http请求中仍然可以读取数据,error()用于显示错误消息
		return new ModelAndView(LIST_ACTION);
	}
	
	private ModelAndView returnEditView(HttpServletRequest request,AcctRole acctRole){
		ModelMap map = new ModelMap();
		map.addAttribute("acctRole", acctRole);
		return new ModelAndView("/AcctRole/edit",map);
	}
	
	/**
	 * 进入更新页面
	 **/
	@RequestMapping(value="/edit")
	public ModelAndView edit(HttpServletRequest request) throws Exception {
		java.lang.Long id = new java.lang.Long(request.getParameter("id"));
		AcctRole acctRole = (AcctRole)acctRoleService.getById(id);
		
		if(acctRole == null){			
			Flash.current().error("error.record.notfound");
			return new ModelAndView(LIST_ACTION);
		}
		
		return returnEditView(request,acctRole);
	}
	
	/**
	 * 保存更新对象
	 **/
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public ModelAndView update(HttpServletRequest request,@Valid AcctRole acctRole,BindingResult result) throws Exception {
		if(result.hasErrors()){
			returnEditView(request,acctRole);
		}
		
		
		acctRoleService.update(acctRole);
		Flash.current().success(UPDATE_SUCCESS);
		return new ModelAndView(LIST_ACTION);
	}
	
	/**
	 *删除对象
	 **/
	@RequestMapping(value="/delete")
	public ModelAndView delete(HttpServletRequest request) {
		String[] items = request.getParameterValues("items");
		for(int i = 0; i < items.length; i++) {
			Hashtable params = HttpUtils.parseQueryString(items[i]);
			
			java.lang.Long id = new java.lang.Long((String)params.get("id"));
			
			acctRoleService.removeById(id);
		}
		Flash.current().success(DELETE_SUCCESS);
		return new ModelAndView(LIST_ACTION);
	}
	
}

